package com.wwj.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wwj.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wwj.gulimall.product.dao.AttrDao;
import com.wwj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wwj.gulimall.product.entity.AttrEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.common.utils.PageUtils;
import com.wwj.common.utils.Query;

import com.wwj.gulimall.product.dao.AttrGroupDao;
import com.wwj.gulimall.product.entity.AttrGroupEntity;
import com.wwj.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    private final AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    private final AttrDao attrDao;
    private final AttrServiceImpl attrService;
    private final AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;

    public AttrGroupServiceImpl(AttrAttrgroupRelationDao attrAttrgroupRelationDao, AttrDao attrDao, AttrServiceImpl attrService, AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService) {
        this.attrAttrgroupRelationDao = attrAttrgroupRelationDao;
        this.attrDao = attrDao;
        this.attrService = attrService;
        this.attrAttrgroupRelationService = attrAttrgroupRelationService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatId(Map<String, Object> params, Long catId) {
        IPage<AttrGroupEntity> page = null;

        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();

        if (catId != 0){
            wrapper.eq(AttrGroupEntity::getCatelogId, catId);
        }

        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq(AttrGroupEntity::getAttrGroupId, key)
                            .or()
                            .like(AttrGroupEntity::getAttrGroupName, key);
            });
        }
        page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
//        if (catId == 0){
//            page = this.page(
//                    new Query<AttrGroupEntity>().getPage(params),
//                    new QueryWrapper<AttrGroupEntity>()
//            );
//        } else {
//            String key = (String) params.get("key");
//            LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(AttrGroupEntity::getCatelogId, catId);
////                    .and((pageTwo)->{
////                        pageTwo.eq(AttrGroupEntity::getAttrGroupId, params.get("key"))
////                                .or()
////                                .like(AttrGroupEntity::getAttrGroupName, params.get("key"));
////                    });
//            if (!key.isEmpty()){
//                wrapper.and((obj)->{
//                    obj.eq(AttrGroupEntity::getAttrGroupId, key)
//                            .or()
//                            .like(AttrGroupEntity::getAttrGroupName, key);
//                });
//            }
//            page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
//        }
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> listAttrByAttrgroupId(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities =
                attrAttrgroupRelationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        if (attrAttrgroupRelationEntities != null && !attrAttrgroupRelationEntities.isEmpty()){
//            List<Long> idsList = new ArrayList<>();
//            for (AttrAttrgroupRelationEntity o : attrAttrgroupRelationEntities) {
//                idsList.add(o.getAttrId());
//            }
            List<Long> idsList = attrAttrgroupRelationEntities.stream().map((attr)->{
                return attr.getAttrId();
            }).collect(Collectors.toList());
            List<AttrEntity> attrEntities = attrDao.selectBatchIds(idsList);
            return attrEntities;
        }
        return Collections.emptyList();
    }

    @Override
    public void removeRelation(AttrAttrgroupRelationEntity[] relationEntities) {
        List<AttrAttrgroupRelationEntity> list = Arrays.asList(relationEntities);
        attrAttrgroupRelationDao.deleteBatchRelation(list);
    }

    @Override
    public PageUtils getAttrNoRelation(Map<String, Object> params, Long attrgroupId) {


        return attrService.getAttrNoRelation(params, attrgroupId);
    }

    @Override
    public void addAttrRelation(AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities) {
        boolean b = attrAttrgroupRelationService.saveBatch(Arrays.asList(attrAttrgroupRelationEntities));
    }


}